using System;
using System.Drawing;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using Direct3D = Microsoft.DirectX.Direct3D;


namespace Cerebrum.Direct3D.Drawing
{
	/// <summary>
	/// Summary description for D3DTextureState.
	/// </summary>
	internal class D3DTextureState
	{
		public D3DTextureState()
		{
			//
			// TODO: Add constructor logic here
			//
		}

		
		private TextureStateManager textureState0;
		private TextureStateManager textureState1;
		private SamplerStateManager samplerState0;
		private RenderStateManager renderState;
		private StateBlock savedStateBlock;
		private StateBlock drawTextStateBlock;


		public void Enter()
		{
			// Set up renderstate
			savedStateBlock.Capture();
			drawTextStateBlock.Apply();
		}

		public void Leave()
		{
			// Restore the modified renderstates
			savedStateBlock.Apply();
		}

		/// <summary>
		/// Initialize the device objects
		/// </summary>
		/// <param name="dev">The grpahics device used to initialize</param>
		public void SetDevice(Device device)
		{
			if (device != null)
			{
				textureState0 = device.TextureState[0];
				textureState1 = device.TextureState[1];
				samplerState0 = device.SamplerState[0];
				renderState = device.RenderState;
			}

		}


		/// <summary>
		/// Restore the font after a device has been reset
		/// </summary>
		public void RestoreDeviceObjects(Device device, Texture texture, bool isZEnable)
		{
			Surface surf = device.GetRenderTarget( 0 );
			bool bSupportsAlphaBlend = Manager.CheckDeviceFormat(device.DeviceCaps.AdapterOrdinal, 
				device.DeviceCaps.DeviceType, device.DisplayMode.Format, 
				Usage.RenderTarget | Usage.QueryPostPixelShaderBlending, ResourceType.Surface, 
				surf.Description.Format );

			// Create the state blocks for rendering text
			for (int which=0; which < 2; which++)
			{
				device.BeginStateBlock();
				if(texture!=null)
					device.SetTexture(0, texture);

				if (isZEnable)
					renderState.ZBufferEnable = true;
				else
					renderState.ZBufferEnable = false;

				if( bSupportsAlphaBlend )
				{
					renderState.AlphaBlendEnable = true;
					renderState.SourceBlend = Blend.SourceAlpha;
					renderState.DestinationBlend = Blend.InvSourceAlpha;
				}
				else
				{
					renderState.AlphaBlendEnable = false;
				}
				renderState.AlphaTestEnable = true;
				renderState.ReferenceAlpha = 0x08;
				renderState.AlphaFunction = Compare.GreaterEqual;
				renderState.FillMode = FillMode.Solid;
				renderState.CullMode = Cull.CounterClockwise;
				renderState.StencilEnable = false;
				renderState.Clipping = true;
				device.ClipPlanes.DisableAll();
				renderState.VertexBlend = VertexBlend.Disable;
				renderState.IndexedVertexBlendEnable = false;
				renderState.FogEnable = false;
				renderState.ColorWriteEnable = ColorWriteEnable.RedGreenBlueAlpha;
				textureState0.ColorOperation = TextureOperation.Modulate;
				textureState0.ColorArgument1 = TextureArgument.TextureColor;
				textureState0.ColorArgument2 = TextureArgument.Diffuse;
				textureState0.AlphaOperation = TextureOperation.Modulate;
				textureState0.AlphaArgument1 = TextureArgument.TextureColor;
				textureState0.AlphaArgument2 = TextureArgument.Diffuse;
				textureState0.TextureCoordinateIndex = 0;
				textureState0.TextureTransform = TextureTransform.Disable; // REVIEW
				textureState1.ColorOperation = TextureOperation.Disable;
				textureState1.AlphaOperation = TextureOperation.Disable;
				samplerState0.MinFilter = TextureFilter.Point;
				samplerState0.MagFilter = TextureFilter.Point;
				samplerState0.MipFilter = TextureFilter.None;

				if (which==0)
					savedStateBlock = device.EndStateBlock();
				else
					drawTextStateBlock = device.EndStateBlock();
			}
		}



	}
}
